package demo6;

import javax.swing.tree.TreeNode;

/**
 * Created with IntelliJ IDEA.
 * Description:二叉搜索树与双向链表
 * User: liangyuanzhang
 * Date: 2025-4-23
 * Time: 0:20
 */
//中序遍历是有序的 root到达一个节点不为空的时候就把它的left和right进行修改
public class Test {

    TreeNode prev = null;
    public void ConvertChild(TreeNode root) {
        if(root == null) return;
        ConvertChild(root.left);

        //打印
        root.left = prev;
        if(prev != null)
            prev.right = root;
        prev = root;

        ConvertChild(root.right);
    }


    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null)
            return null;
        ConvertChild(pRootOfTree);

        TreeNode head = pRootOfTree;
        while(head.left != null) {
            head = head.left;
        }
        return head;
    }
}

